package services

import (
	"api/app/wms/models"
	"gitee.com/dreamwood/ez"
)

func UpdateItemStorage(item *models.ItemSku) {
	rep := models.NewInLogCrud()
	rep.Factory.SetAssociations("Item")
	logs, _ := rep.FindBy(ez.M{
		"itemId":      item.Id,
		"numLeft__gt": 0,
	}, nil, 0, 0)
	var left = 0.0
	for _, log := range logs {
		left += log.NumLeft * log.Item.UnitBase / log.Item.UnitExchange
	}
	//检查是否有物料在档没有则建立
	repStore := models.NewItemMainCrud()
	find, e := repStore.FindOneBy(ez.M{"itemId": item.Id}, nil)
	ez.PrintError(e)
	if find.Id == 0 {
		find.Item = item
		find.Num = 0
	}
	find.SkuCode = item.Code
	find.SkuText = item.Text
	find.Num = left
	find.Save(false)
}
